package com.mb.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbOrder;
import com.mb.app.domain.TbOrderPay;
import com.mb.app.domain.TbOrderReturn1;
import com.mb.app.dao.TbOrderReturn1Mapper;
import com.mb.app.service.ITbOrderPayService;
import com.mb.app.service.ITbOrderReturn1Service;
import com.mb.app.service.ITbOrderService;
import com.mb.pay.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单退款信息  服务实现类
 * </p>
 *
 * @author zwl
 * @since 2019-04-22
 */
@Service
@Slf4j
public class TbOrderReturn1ServiceImpl extends ServiceImpl<TbOrderReturn1Mapper, TbOrderReturn1> implements ITbOrderReturn1Service {

    @Autowired
    private IPayService payService;

    @Autowired
    private ITbOrderPayService orderPayService;

    @Autowired
    private ITbOrderService orderService;

    @Override
    public List<TbOrderReturn1> queryList(Map<String, Object> map) {
        List<TbOrderReturn1> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(TbOrderReturn1 TbOrderReturn1) {
        int  istre = 0;
            TbOrderReturn1 orderReturn1 = this.baseMapper.selectById(TbOrderReturn1.getReturnOrderId());
            if (orderReturn1 != null) {
                if (TbOrderReturn1.getReturnOrderStatus() == 5) {
                    TbOrderReturn1.setFinishReturnTime(new Date());
                }

                // 如果未发货申请退款，退款驳回 修改订单状态为 1 ，订单详情状态为 9
                TbOrder tbOrder = orderService.selectById(orderReturn1.getOrderId());
                if (tbOrder != null) {
                    if (TbOrderReturn1.getReturnOrderStatus() == 2 && tbOrder.getOrderStatus() == 1) {
                        TbOrderReturn1.setReturnOrderStatus(9);
                    }else if (TbOrderReturn1.getReturnOrderStatus() == 1 && tbOrder.getOrderStatus() == 1 && orderReturn1.getReturnType() == 2) {
                        orderReturn1.setReturnOrderStatus(5);
                        istre = this.refundPay(orderReturn1);
                        TbOrderReturn1.setReturnOrderStatus(5);
                    }else if (orderReturn1.getReturnType() == 2 && TbOrderReturn1.getReturnOrderStatus() == 5 && tbOrder.getOrderStatus() == 3) {
                        orderReturn1.setReturnOrderStatus(5);
                        istre = this.refundPay(orderReturn1);
                    }else {
                        istre = 1;
                    }
                }
                if(istre == 1) {
                    // 修改订单详情状态
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put("orderReturnId",TbOrderReturn1.getReturnOrderId());
                    List<Map<String,Object>> returnOrderItem = this.baseMapper.selectReturnOrderItem(map);
                    if (returnOrderItem != null && returnOrderItem.size() != 0) {
                        for (int i =0; i < returnOrderItem.size(); i++) {
                            Map<String,Object> returnOrderMap = returnOrderItem.get(i);
                            map.put("orderId",returnOrderMap.get("orderId"));
                            map.put("productId",returnOrderMap.get("productId"));
                            Map<String,Object> orderItem = orderService.selectOrderItem(map);
                            if (orderItem != null) {
                                orderItem.put("id",orderItem.get("id"));
                                orderItem.put("returnOrderStatus",TbOrderReturn1.getReturnOrderStatus());
                                istre = orderService.updateItem(orderItem);
                            }
                        }
                    }
                    // 修改退款订单状态
                    istre = this.baseMapper.updateById(TbOrderReturn1);
                }
//                // 退款
//                if (orderReturn1.getReturnType() == 2){
//                    if (TbOrderReturn1.getReturnOrderStatus() == 5) {
//                        orderReturn1.setReturnOrderStatus(5);
//                        istre = this.refundPay(orderReturn1);
//                    }
//                }
//                // 修改退款订单状态
//                istre = this.baseMapper.updateById(TbOrderReturn1);
            }
        return istre;
    }

    @Override
    public TbOrderReturn1 getDtl(String orderReturnId) {
        TbOrderReturn1 orderReturn1 = this.baseMapper.selectByIds(orderReturnId);
        return orderReturn1;
    }

    public int  refundPay(TbOrderReturn1 TbOrderReturn1) {
        int istre = 0;
        try {
            // 退款接口
            if (TbOrderReturn1.getReturnOrderStatus() == 5) {
                TbOrderPay orderPay = orderPayService.selectByOrderPay(TbOrderReturn1.getOrderId());
                if (orderPay != null) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("payOrderId", orderPay.getPayOrderId());
                    map.put("payMode", orderPay.getPayMethod());
                    map.put("refundAmt", TbOrderReturn1.getReturnTotalAmt());
//                      map.put("refundAmt",new BigDecimal("0.01"));
                      map.put("returnOrderId",TbOrderReturn1.getReturnOrderId());
                    JSONObject jsonObject = payService.RefundPay(map);
                    if (1 == jsonObject.getInteger("code")) {
                        log.info("退款失败");
                        istre = 0;
                        try {
                            int i = 1 / 0;
                        } catch (Exception e) {
                            log.error("Exception",e);
                            throw e;
                        }
                    }else {
                        istre = 1;
                    }
                }else {
                    try {
                        int i = 1 / 0;
                    } catch (Exception e) {
                        log.error("Exception",e);
                        throw e;
                    }
                }
            }
        } catch (Exception e) {
            log.error("Exception",e);
            throw e;
        }
        return  istre;
    }


    /***************************************************************信息维护*******************************************************************************************/

    @Override
    public List<HashMap<String, Object>> queryListPar(Map<String, Object> map)
    {
        List<HashMap<String, Object>> list = this.baseMapper.lstPar(map);
        return list;
    }

    @Override
    public int countPar(Map<String, Object> map)
    {
        int count = this.baseMapper.countPar(map);
        return count;
    }

    @Override
    public Map<String, Object> selectParAll(String id)
    {
        Map<String, Object> list = this.baseMapper.selectParAll(id);
        return list;
    }

    @Override
    public int updatePar(Map<String, Object> map)
    {
        int updatePar = this.baseMapper.updatePar(map);
        return updatePar;
    }


}
